﻿<%
Function forSave(kl)
    If InStr(1, LCase(kl), "select") > 0 And InStr(1, LCase(kl), "from") > 0 Then
        kl = Replace(kl, "select", "")
        kl = Replace(kl, "from", "")
        kl = Replace(kl, "where", "")
        kl = Replace(kl, "delete", "")
    End If
    kl = Replace(kl, "'", "''")
    forSave = kl
End Function

Function gSave(s) '[自定义函数]
    s = Replace(s, "'", "''")
    gSave = s
End Function

Function iNull(s)
    If IsNull(s) Then
        iNull = "没有留下信息"
    ElseIf s = "" Then
        iNull = "没有留下信息"
    Else
        iNull = s
    End If
End Function

Function nNull(s)
    If IsNull(s) Then
        nNull = 0
    ElseIf s = "" Then
        nNull = 0
    Else
        nNull = CLng(s)
    End If
End Function

Function byNull(s)
    If IsNull(s) Then
        byNull = ""
    ElseIf s = "" Then
        byNull = ""
    Else
        byNull = s
    End If
End Function

Function WriteToFile(fname, txtcontent)
    Dim obj
    Dim objfile
    Dim tm
    set obj = Server.CreateObject("Scripting.FileSystemObject")
    objfile = obj.OpenTextFile(Server.MapPath(fname), 2, True)
    objfile.WriteLine(txtcontent)
    objfile.close()
    set objfile = Nothing
    set obj = Nothing
End Function

Function ReadFile(fname)
    Dim obj
    Dim objfile
    Dim fullcode
    set obj = Server.CreateObject("Scripting.FileSystemObject")
    set objfile = obj.OpenTextFile(Server.MapPath(fname), 1, True)
    fullcode=objfile.ReadAll()
    objfile.close()
    set objfile = Nothing
    set obj = Nothing
    ReadFile=fullcode
End Function

Function ReadAnyTextFile(sFile,sCharset)
	If Len(sFile) = 0 Then Exit Function
	Dim ados
	Set ados =Server.CreateObject("adodb.stream")
	ados.Charset = sCharset 'utf-8，Unicode。
	ados.Type = 2
	ados.Open
	ados.LoadFromFile sFile
	ados.Position = 0
	ReadAnyTextFile = ados.ReadText '一次性读取所有文本，可以设置读取文本数量，自己查询ADODB.STREAM对象
	ados.Close
	Set ados = Nothing
End Function

Function SaveAnyTextFile(sCode,sFile,sCharset)
	If Len(sFile) = 0 Then Exit Function
	Dim ados
	Set ados =Server.CreateObject("adodb.stream")
	ados.Charset = sCharset 'utf-8，Unicode。
	ados.Type = 2
	ados.Open
	ados.WriteText sCode
	ados.Position = 0
	ados.SaveToFile sFile,2 '一次性读取所有文本，可以设置读取文本数量，自己查询ADODB.STREAM对象
	ados.Close
	Set ados = Nothing
End Function

Sub ShowStr(s)
    Response.Write(s)
End Sub

Function repeatStr(sInStr, nTimes)
    Dim i
    Dim sOut
    If isnumeric(nTimes) <> True Then
        nTimes = 1
    End If
    sOut = ""
    For i = 1 To nTimes
        sOut = sOut & sInStr
    Next
    repeatStr = sOut
End Function

Function FixStr(s)
    If Len(s) < 2 Then
        s = "0" & s
    End If
    FixStr = s
End Function

Function MakeUpStr(sStr, nLimit, sMakeUp, nPlace)
    'nPlace表示在前面补（0）还是后面补（1）
    If len(sStr) < nLimit Then
        If nPlace = 0 Then
            sStr = repeatStr(sMakeUp, nLimit - len(sStr)) & sStr
        Else
            sStr = sStr & repeatStr(sMakeUp, nLimit - len(sStr))
        End If
    End If
    MakeUpStr = sStr
End Function

Function formattime(sTime, sFormat)
    Dim dTime
    dTime = CDate(sTime)
    Select Case sFormat
        Case "yyyymmdd"
            formattime = MakeUpStr(year(dTime), 4, "0", 0) & MakeUpStr(month(dTime), 2, "0", 0) & MakeUpStr(day(dTime), 2, "0", 0)
        Case "yyyy-mm-dd"
            formattime = MakeUpStr(year(dTime), 4, "0", 0) & "-" & MakeUpStr(month(dTime), 2, "0", 0) & "-" & MakeUpStr(day(dTime), 2, "0", 0)
		Case "yyyy-mm-dd hh:MM:ss"
			formattime = MakeUpStr(year(dTime), 4, "0", 0) & "-" & MakeUpStr(month(dTime), 2, "0", 0) & "-" & MakeUpStr(day(dTime), 2, "0", 0)
			formattime = formattime & " " & MakeUpStr(hour(dTime), 2, "0", 0) & ":" & MakeUpStr(minute(dTime), 2, "0", 0) & ":" & MakeUpStr(second(dTime), 2, "0", 0)
    End Select
End Function

Sub retHtml(url) '获取后，重定向数据
    set xh = CreateObject("Microsoft.XMLHTTP")
    xh.Open "GET", url, 0
    xh.Send()
    set MyStream = CreateObject("Adodb.Stream")
    MyStream.Type = 1
    MyStream.Open()
    MyStream.Write(xh.responseBody)
    MyStream.Position = 0
    response.Clear()
    response.BinaryWrite(MyStream.Read)
    Response.End()
End Sub

Function getHtml(url) '以HTML格式备份
    set xh = CreateObject("Microsoft.XMLHTTP")
    xh.Open "GET", url, 0
    xh.Send()
    set MyStream = CreateObject("Adodb.Stream")
    MyStream.Type = 2
    MyStream.Open()
    MyStream.WriteText(xh.responseText)
    MyStream.Position = 0
    getHtml = MyStream.ReadText
    MyStream.Close()
End Function

function createRst(obj)
    set obj=server.CreateObject("adodb.recordset")
end function

Function adminLogin()
    If Session("fix_loginrole") = "admin" Then
        adminLogin = True
        Exit Function
    End If
    adminLogin = False
End Function

Function officerLogin()
    If Session("fix_loginrole") = "officer" Then
        officerLogin = True
        Exit Function
    End If
    officerLogin = False
End Function

Function isLogin()
    If adminLogin = True Or officerLogin = True Then
        isLogin = True
    Else
        isLogin = False
    End If
End Function

Function getUndoneNum()  '获取本人未处理的任务
    Dim r
    createRst r
    r.open "select count(*) as Undone from s_request left join s_accept on s_accept.RequestID=s_request.id where s_request.state='taken' and s_accept.resp_boycode='" & Session("fix_usercode") & "'", conn, 3, 3
    getUndoneNum = r("Undone").Value
    r.close()
    set r = Nothing
End Function
Function NewRequest(QueryString)
    Dim sQs
    sQs = Request(QueryString)
    NewRequest = gSave(sQs)
End Function
Function TransState(state)
    Select Case state
        Case "done"
            TransState = "已完成"
        Case "taken"
            TransState = "处理中"
        Case "refuse"
            TransState = "已拒绝"
        Case "wait"
            TransState = "未处理"
        Case "return"
            TransState = "自动重新发起申请"
        Case Else
            TransState = "未知"
    End Select
End Function

Function ShowState(state)
    Select Case state
        Case "done"
            ShowState = "完成维修处理，归档"
        Case "taken"
            ShowState = "干事已承接请求"
        Case "refuse"
            ShowState = "此干事已请辞此事"
        Case "wait"
            ShowState = "已提交，尚未处理"
        Case "return"
            ShowState = "重新发起申请"
        Case Else
            ShowState = "未知的操作"
    End Select
End Function

Function GetAdminInfo(usercode, fieldname)
    Dim nfuck
    If usercode = "poster" Then
        If isLogin = True Then
            GetAdminInfo = "[用户]"
        Else
            GetAdminInfo = "您"
        End If
        Exit Function
    End If
    set nfuck = Server.CreateObject("adodb.recordset")
    nfuck.open "select id," & fieldname & " from s_admin where ad_code='" & usercode & "'", conn, 3, 3
    If nfuck.recordcount = 0 Then
        GetAdminInfo = ""
        nfuck.close()
        set nfuck = Nothing
        Exit Function
    End If
    GetAdminInfo = nfuck(fieldname)
    nfuck.close()
    set nfuck = Nothing
End Function

Function WriteIt(s)
    Response.Clear()
    Response.Write(s)
    Response.End()
End Function

Function ExcuteSql(sql)
    Dim MyEx
    set MyEx = Server.CreateObject("adodb.recordset")
    MyEx.open sql, conn, 3, 3
    set MyEx = Nothing
End Function
Function getAcceptID(reqID)
    Dim rtp
    set rtp = Server.CreateObject("adodb.recordset")
    rtp.open "select id from s_accept where RequestID=" & reqID, conn, 3, 3
    If rtp.recordcount = 0 Then
        rtp.close()
        set rtp = Nothing
        getAcceptID = 0
        Exit Function
    End If
    getAcceptID = rtp("id")
    rtp.close()
    set rtp = Nothing
End Function
Function getReqID(acceptID)
    Dim rtp
    set rtp = Server.CreateObject("adodb.recordset")
    rtp.open "select RequestID from s_accept where id=" & acceptID, conn, 3, 3
    If rtp.recordcount = 0 Then
        rtp.close()
        set rtp = Nothing
        getReqID = 0
        Exit Function
    End If
    getReqID = rtp("RequestID")
    rtp.close()
    set rtp = Nothing
End Function
Function getRequestField(reqID, fieldName)
    Dim rf, sql
    set rf = Server.CreateObject("adodb.recordset")
    sql = "select " & fieldName & " from s_request where id=" & reqID
    rf.open sql, conn, 3, 3
    getRequestField = rf(fieldName)
    rf.close()
    set rf = Nothing
End Function
Function getInfo(sTable, fField, fValue, gField)
    Dim rf, sql
    set rf = Server.CreateObject("adodb.recordset")
    sql = "select " & gField & " from " & sTable & " where " & fField & "=" & fValue
    rf.open sql, conn, 3, 3
    getInfo = rf(gField)
    rf.close()
    set rf = Nothing
End Function
Sub alterInfo(sTable, fField, fValue, sField,sValue)
    Dim rf, sql
    set rf = Server.CreateObject("adodb.recordset")
    sql = "update " & sTable & " set [" & sField & "]=" & sValue & " where " & fField & "=" & fValue
    rf.open sql, conn, 3, 3
    set rf = Nothing
End Sub

%>